import Cookies from 'js-cookie'

const AppId = 'appid'

export default {
    namespaced: true,
    state: {
        ACCOUNT_TYPES:[
          { value:1, label:'订阅号' },
          { value:2, label:'服务号' }
        ],
        accountList:[],
        selectedAppid:''
    },
    mutations: {
      updateAccountList (state, list) {
        state.accountList = list
        if(!list.length)return
        if(!state.selectedAppid){
          let appIdCookie  = getAppId() //Vue.cookie.get('appid')
          let selectedAppId = appIdCookie?appIdCookie:list[0].appid
          this.commit('wxAccount/selectAccount',selectedAppId)
        }
      },
      selectAccount (state, appid) {
        // Vue.cookie.set('appid',appid)
        setAppId(appid)
        let oldAppId = state.selectedAppid
        state.selectedAppid = appid
        if(oldAppId){//切换账号时刷新网页
          location.reload();
        }
      },
    }
  }


export function getAppId() {
  return Cookies.get(AppId)
}

export function setAppId(appId) {
  return Cookies.set(AppId, appId)
}

export function removeAppId() {
  return Cookies.remove(AppId)
}
